package com.dfbz.demo04_Enum类;

import org.junit.Test;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo01 {
    Season spring = Season.SPRING;              // ordinal:0
    Season summer = Season.SUMMER;              // ordinal:1
    Season autumn = Season.AUTUMN;              // ordinal:2
    Season winter = Season.WINTER;              // ordinal:3


    @Test
    public void test1() throws Exception {

        System.out.println(spring.compareTo(spring));           // 0-0=0
        System.out.println(spring.compareTo(summer));           // 0-1=-1
        System.out.println(winter.compareTo(summer));           // 3-1=2
        System.out.println(winter.compareTo(spring));           // 3-0=3

    }


    @Test
    public void test2() {

        Season spring2 = Season.SPRING;

        System.out.println(spring == spring2);                  // true
        System.out.println(spring.equals(spring2));             // 枚举对象的equals方法比较的是内存地址值(返回true)
    }

    @Test
    public void test3() {
        Class<Season> clazz = spring.getDeclaringClass();
        Class<? extends Season> clazz2 = spring.getClass();

        System.out.println(clazz == clazz2);            // true
    }

    @Test
    public void test4() {

        int hashCode = spring.hashCode();
        System.out.println(hashCode);           // 2027961269

        String name = spring.name();
        System.out.println(name);               // 名字就是枚举项的名字,SPRING

        int ordinal_spring = spring.ordinal();
        int ordinal_summer = summer.ordinal();
        int ordinal_autumn = autumn.ordinal();
        int ordinal_winter = winter.ordinal();

        System.out.println(ordinal_spring);         // 0
        System.out.println(ordinal_summer);         // 1
        System.out.println(ordinal_autumn);         // 2
        System.out.println(ordinal_winter);         // 3
    }

    @Test
    public void test5() {


        Season spring = Season.SPRING;

        Season s = Season.valueOf("SPRING");        // 通过枚举项的名称获取枚举项

        System.out.println(s == spring);            // true(返回的还是同一个枚举项)
    }


    @Test
    public void test6() {

        // 获取这个枚举类中所有的枚举项
        Season[] values = Season.values();
        for (Season value : values) {
            System.out.println(value);
        }

    }
}
